package com.cater.sys.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.CompanyBook;
import com.cater.sys.entity.SysUserpoint;
import com.cater.sys.entity.SysUserpointLog;
import com.cater.sys.service.CompanyBookServiceImpl;
import com.cater.sys.service.SysUserpointLogServiceImpl;
import com.cater.sys.service.SysUserpointServiceImpl;
import com.cater.utils.UploadUtil;

/**
 * 描述：企业通讯录控制层
 * 
 * @author yong
 * @date 2019/03/18
 */
@Controller
@RequestMapping("/companyBook")
public class CompanyBookController {

	@Autowired
	private CompanyBookServiceImpl service;
	@Autowired
	private SysUserpointServiceImpl userpointService;
	@Autowired
	private SysUserpointLogServiceImpl userpointLogService;

	/**
	 * @企业通讯录管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "company/companyBook/list";
	}

	/**
	 * @企业通讯录管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询企业通讯录分页数据")
	public PageResult<CompanyBook> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<CompanyBook> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("companyBook:add")
	public String addView(Model model) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		model.addAttribute("nowYear", sdf.format(new Date()));
		return "company/companyBook/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("companyBook:add")
	@ResponseBody
	@SystemControllerLog(description = "添加企业通讯录")
	public JSONObject add(CompanyBook t, CommonQuery query) {
		JSONObject json = new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		SysUserpoint userpoint = null;
		SysUserpointLog userpointLog = null;
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			Integer years = Integer.valueOf(t.getYears());
			Integer nowYear = Integer.valueOf(sdf.format(new Date()));
			Integer score = 0;
			Integer yearNum = nowYear - years;
			if (yearNum < 2) {
				score = 10;
			} else if (yearNum >= 2 && yearNum <= 5) {
				score = 8;
			} else {
				score = 3;
			}
			userpoint = new SysUserpoint();
			userpoint = userpointService.getBy(query);
			if (userpoint != null) {
				userpointLog = new SysUserpointLog();
				Integer old = userpoint.getIntegral();
				userpointLog.setOldIntegral(old);
				userpointLog.setNowIntegral(old + score);
				userpoint.setIntegral(old + score);
				userpointService.update(userpoint);
			} else {
				userpoint = new SysUserpoint();
				userpoint.setIntegral(score);
				userpointService.saves(userpoint);
				userpointLog = new SysUserpointLog();
				userpointLog.setOldIntegral(0);
				userpointLog.setNowIntegral(score);
			}
			userpointLog.setSource("上传文档年限在" + yearNum + "年，增加" + score + "积分");
			userpointLog.setChangeValue(score);
			userpointLogService.saves(userpointLog);
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("companyBook:edit")
	public String editView(String id, Model model, CommonQuery query) {
		CompanyBook t = service.getById(id);
		model.addAttribute("companyBook", t);
		return "company/companyBook/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("companyBook:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改企业通讯录")
	public JSONObject update(CompanyBook t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("companyBook:delete")
	@SystemControllerLog(description = "删除企业通讯录")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("companyBook:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		CompanyBook t = service.getById(id);
		model.addAttribute("companyBook", t);
		return "company/companyBook/detail";
	}

	@RequestMapping(value = "download", method = RequestMethod.GET)
	@ResponseBody
	public void downloadFile(HttpServletRequest req, HttpServletResponse resp, String id) {
		CompanyBook t = service.getById(id);
		if (t != null) {
			// 真实文件名
			String filePath = t.getFilePath();
			// 进行转码后的文件名，用来下载之后的文件名
			UploadUtil.download(resp, req, filePath, t.getCompanyName());
		}
	}
}